package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("index")
    public  String  index(){
        return "permission/list";
    }

    @RequiresPermissions("permission:list")
    @PermissionName("权限列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryForObject qo){
        List<Permission> list = permissionService.list();
        PageResult p = new PageResult();
        p.setTotal(list.size());
        p.setRows(list);
        return p;
    }

    @RequestMapping("queryPermissions")
    @ResponseBody
    public List<Permission> queryPermissions(QueryForObject qo){
        return permissionService.list();
    }

    @RequestMapping("reload")
    @ResponseBody
    public JsonResult reload(){
    	JsonResult result = new JsonResult();
    	try {
			permissionService.reload();
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult("加载权限失败");
		}
    	return result;
    }
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
    	JsonResult result = new JsonResult();
    	try {
			permissionService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult("删除权限失败");
		}
    	return result;
    }

    @RequestMapping("queryPermissionByRoleId")
    @ResponseBody
    public  List<Permission>  queryPermissionByRoleId(Long roleId){
        return permissionService.queryPermissionByRoleId(roleId);
    }
}
